package com.nuo.backend.modules.security.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.backend.modules.security.annotation.SecMenu;
import com.nuo.backend.modules.security.entity.*;
import com.nuo.backend.modules.security.service.SecRoleMenuService;
import com.nuo.backend.modules.security.service.SecUserRoleService;
import com.nuo.backend.modules.security.service.SecUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nuo.backend.modules.security.service.SecRoleService;
import com.nuo.backend.common.utils.PageUtils;
import com.nuo.backend.common.utils.R;



/**
 * 角色
 *
 * @author nuo
 * @email 3033616351@qq.com
 * @date 2022-12-04 01:55:55
 */
@RestController
@RequestMapping("sec/role")
public class SecRoleController {
    @Autowired
    private SecRoleService secRoleService;
    @Autowired
    private SecRoleMenuService secRoleMenuService;
    @Autowired
    private SecUserService secUserService;
    @Autowired
    private SecUserRoleService secUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        SecUserEntity secUser = (SecUserEntity) secUserService.getUserBySec().get("user");
        Set<Long> roleIdSet = secUser.getSecRoleList().stream().map(SecRoleEntity::getRoleId).collect(Collectors.toSet());
        // TODO root 获取全部角色的用户, 普通用户获取自己创建的角色
        if (!roleIdSet.contains(1L)) {
            params.put("createUserId", secUser.getUserId());
        }
        PageUtils page = secRoleService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{roleId}")
    public R info(@PathVariable("roleId") Long roleId){
        return R.ok().put("secRole", secRoleService.getById(roleId));
    }

    /**
     * 保存
     */
    @SecMenu
    @RequestMapping("/save")
    public R save(@RequestBody SecRoleEntity secRole){
        secRole.setCreateUserId(secUserService.getUserId());
		secRoleService.save(secRole);
        Set<SecRoleMenuEntity> secRoleMenuSet = secRole.getSecMenuList().stream().map(i -> new SecRoleMenuEntity(null, secRole.getRoleId(), i.getMenuId())).collect(Collectors.toSet());
        secRoleMenuService.saveBatch(secRoleMenuSet);
        return R.ok();
    }

    /**
     * 修改
     */
    @SecMenu
    @RequestMapping("/update")
        public R update(@RequestBody SecRoleEntity secRole){
		secRoleService.updateById(secRole);
        secRoleMenuService.remove(new LambdaQueryWrapper<SecRoleMenuEntity>().eq(SecRoleMenuEntity::getRoleId, secRole.getRoleId()));
        Set<SecRoleMenuEntity> secRoleMenuSet = secRole.getSecMenuList().stream().map(i -> new SecRoleMenuEntity(null, secRole.getRoleId(), i.getMenuId())).collect(Collectors.toSet());
        secRoleMenuService.saveBatch(secRoleMenuSet);
        secUserService.clearUserAuthorityBySecRoleIdList(Collections.singletonList(secRole.getRoleId()));
        return R.ok();
    }

    /**
     * 删除
     */
    @SecMenu
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] roleIds){
        // TODO 清缓存
        secUserService.clearUserAuthorityBySecRoleIdList(Arrays.asList(roleIds));

		secRoleService.removeByIds(Arrays.asList(roleIds));
        secUserRoleService.remove(new LambdaQueryWrapper<SecUserRoleEntity>().in(SecUserRoleEntity::getRoleId, Arrays.asList(roleIds)));
        secRoleMenuService.remove(new LambdaQueryWrapper<SecRoleMenuEntity>().in(SecRoleMenuEntity::getRoleId, Arrays.asList(roleIds)));
        return R.ok();
    }

}
